package com.example.roundcornerprogressbar.slice;

import com.example.roundcornerprogressbar.ResourceTable;
import com.example.roundcornerprogressbar.view.progressbar.TextRoundCornerProgressBar;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.global.resource.ResourceManager;

public class TextFraction extends Fraction {
    private ResourceManager resManager;
    private Component component;

    public final static int GRAVITY_START = 0;
    public final static int GRAVITY_END = 1;
    public final static int PRIORITY_INSIDE = 0;
    public final static int PRIORITY_OUTSIDE = 1;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_ability_text, container, false);
        resManager = component.getResourceManager();
        initView();
        return component;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    private void initView() {
        TextRoundCornerProgressBar roundCornerProgressBar = (TextRoundCornerProgressBar) component.findComponentById(ResourceTable.Id_progressBarTextCustom);
        roundCornerProgressBar.setOnProgressChangedListener((view, progress, isPrimaryProgress, isSecondaryProgress) -> {
            if (isPrimaryProgress) {
                roundCornerProgressBar.setProgressText(String.format("%.0f", roundCornerProgressBar.getProgress()));
            }
        });
        if (null != roundCornerProgressBar) {
            setProgressBarColor(roundCornerProgressBar);
            roundCornerProgressBar.setBasePadding(4);
            roundCornerProgressBar.setMax(255);
            roundCornerProgressBar.setProgress(80);
            roundCornerProgressBar.setRadius(20);
            roundCornerProgressBar.setProgressText("80");
            roundCornerProgressBar.setTextInsideGravity(GRAVITY_START);
            roundCornerProgressBar.setTextOutsideGravity(GRAVITY_START);
            roundCornerProgressBar.setTextPositionPriority(PRIORITY_INSIDE);
        }

        Button btDecrease = (Button)component.findComponentById(ResourceTable.Id_buttonDecrease);
        btDecrease.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                roundCornerProgressBar.setProgress(roundCornerProgressBar.getProgress() - 20);
            }
        });

        Button btAdd = (Button)component.findComponentById(ResourceTable.Id_buttonAdd);
        btAdd.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                roundCornerProgressBar.setProgress(roundCornerProgressBar.getProgress() + 20);
            }
        });

        RadioContainer insideGravityContainer = (RadioContainer)component.findComponentById(ResourceTable.Id_radioGroupInsideGravity);
        insideGravityContainer.setMarkChangedListener((radioContainer, i) -> {
            switch (i) {
                case 0:
                    roundCornerProgressBar.setTextInsideGravity(GRAVITY_START);
                    break;
                case 1:
                    roundCornerProgressBar.setTextInsideGravity(GRAVITY_END);
                    break;
            }
        });

        RadioContainer outsideGravityContainer = (RadioContainer)component.findComponentById(ResourceTable.Id_radioGroupOutsideGravity);
        outsideGravityContainer.setMarkChangedListener((radioContainer, i) -> {
            switch (i) {
                case 0:
                    roundCornerProgressBar.setTextOutsideGravity(GRAVITY_START);
                    break;
                case 1:
                    roundCornerProgressBar.setTextOutsideGravity(GRAVITY_END);
                    break;
            }
        });

        RadioContainer positionPriorityContainer = (RadioContainer)component.findComponentById(ResourceTable.Id_radioGroupTextPositionPriority);
        positionPriorityContainer.setMarkChangedListener((radioContainer, i) -> {
            switch (i) {
                case 0:
                    roundCornerProgressBar.setTextPositionPriority(PRIORITY_INSIDE);
                    break;
                case 1:
                    roundCornerProgressBar.setTextPositionPriority(PRIORITY_OUTSIDE);
                    break;
            }
        });
    }

    private void setProgressBarColor(TextRoundCornerProgressBar roundCornerProgressBar) {
        try {
            roundCornerProgressBar.setProgressBackgroundColor(resManager.getElement(ResourceTable.Color_sample_progress_background).getColor());
            roundCornerProgressBar.setTextProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_text_color).getColor());
            roundCornerProgressBar.setProgressColor(resManager.getElement(ResourceTable.Color_sample_progress_primary).getColor());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
